package com.siyu.tickets.dao.impl;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.siyu.common.hibernate3.Finder;
import com.siyu.common.hibernate3.HibernateBaseDao;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.PayFlowDao;
import com.siyu.tickets.entity.PayFlow;

@Repository
public class PayFlowDaoImpl extends HibernateBaseDao<PayFlow, Integer> implements PayFlowDao {

	public Pagination getPage(Integer userId, Integer operatorId, String type, Date beginTime, Date endTime,
			int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from PayFlow bean");
		f.append(" where 1=1");
		if (userId != null) {
			f.append(" and bean.user.id=:userId ");
			f.setParam("userId", userId);
		}
		if (operatorId != null) {
			f.append(" and bean.operator.id=:operatorId ");
			f.setParam("operatorId", operatorId);
		}
		if (!StringUtils.isBlank(type)) {
			f.append(" and bean.action=:type ");
			f.setParam("type", type);
		}
		if (beginTime != null) {
			f.append(" and bean.createTime >= :beginTime");
			f.setParam("beginTime", beginTime);
		}
		if (endTime != null) {// 由于传入的传入的日期不带时间，所以这儿要做日期+1处理
			Calendar cal = Calendar.getInstance();
			cal.setTime(endTime);
			cal.add(Calendar.DAY_OF_YEAR, 1);
			endTime = cal.getTime();
			f.append(" and bean.createTime <= :endTime");
			f.setParam("endTime", endTime);
		}
		f.append(" order by bean.createTime desc");
		return find(f, pageNo, pageSize);
	}

	public PayFlow findById(Integer id) {
		PayFlow entity = get(id);
		return entity;
	}

	public PayFlow save(PayFlow bean) {
		getSession().save(bean);
		return bean;
	}

	public PayFlow deleteById(Integer id) {
		PayFlow entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	protected Class<PayFlow> getEntityClass() {
		return PayFlow.class;
	}

}
